package com.base.controller.api;

import com.base.helper.ApiinfoesHelper;
import com.base.mapper.voipmapper.ChangeRecordMapper;
import com.base.po.acess.AcessToken;
import com.base.po.acess.TokenUtils;
import com.base.po.param.NetState;
import com.base.po.voip.Apiinfoes;
import com.base.po.voip.ChangeRecord;
import com.base.po.voip.Rewards;
import com.base.po.voip.Subscribers;
import com.base.service.ChangeRecordService;
import com.base.service.RewardsService;
import com.base.service.SubscribersService;
import com.base.utils.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 资金变更明细
 */
@Controller
@RequestMapping("/record")
@Api(tags = "资金变更明细")
public class ChangeRecordController {

    private final SubscribersService subscribersService;

    private final ChangeRecordService changeRecordService;

    private final RewardsService rewardsService;

    private final ChangeRecordMapper changeRecordMapper;

    public ChangeRecordController(SubscribersService subscribersService,
                                  ChangeRecordService changeRecordService,
                                  RewardsService rewardsService,
                                  ChangeRecordMapper changeRecordMapper) {
        this.subscribersService = subscribersService;
        this.changeRecordService = changeRecordService;
        this.rewardsService = rewardsService;
        this.changeRecordMapper = changeRecordMapper;
    }

    /**
     * 话费明细
     *
     * @param userid  用户id
     * @param curPage 页码
     */
    @ApiOperation(value="话费明细")
    @RequestMapping(value = "/moneyRecord", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public NetState shopMoneyRecord(HttpServletRequest request, String userid, Integer type, Integer curPage) {
        // 网络状态
        NetState netState = new NetState();
        // 如果参数为空 则限制执行
        if (userid == null || userid.equals("")) {
            netState.setErrorCode(NetState.ACCOUNT_ERROR);
            netState.setData("参数丢失");
        } else {
            com.github.pagehelper.Page<ChangeRecord> page = PageHelper.startPage(curPage == null ? 1 : curPage, 10);
            List<ChangeRecord> record  = changeRecordMapper.selectByMoneys(userid, type);
            Map<String, Object> map = new HashMap<>();
            map.put("total", page.getTotal());
            map.put("rows", record);
            netState.setErrorCode(NetState.SUCCESS_CONN);
            netState.setData("查询成功");
            netState.setJson(map);
        }
        return netState;
    }

    /**
     * 话费明细
     *
     * @param curPage 页码
     */
    @RequestMapping(value = "/moneyRecordForToken", method = {RequestMethod.POST, RequestMethod.GET})
    public @ResponseBody
    NetState moneyRecordForToken(HttpServletRequest request, Integer type, Integer curPage)
            throws Exception {
        // 网络状态
        NetState netState = new NetState();

        String AuthorizationToken = request.getHeader("SDB-Authorization");
        AcessToken acessToken = null;
        if (AuthorizationToken != null && !AuthorizationToken.equals("")){
            acessToken = TokenUtils.getAcessToken(AuthorizationToken);
        }
        // 如果参数为空 则限制执行
        if (acessToken == null) {
            netState.setErrorCode(NetState.SIGN_ERROR);
            netState.setData("数据签名错误");
        } else {
            String mobile = acessToken.getMobile();
            String parentId = acessToken.getParentid();
            // 验证参数
            if (mobile == null || mobile.equals("") || parentId == null || parentId.equals("")) {
                netState.setErrorCode(NetState.PARAMS_ERROR);
                netState.setData("客户端参数错误");
                return netState;
            }
            // 查询用户
            List<Subscribers> subscribers = subscribersService.selectBySub(mobile, parentId);
            if (subscribers.isEmpty()){
                netState.setErrorCode(NetState.PARAMS_ERROR);
                netState.setData("用户数据错误");
                return netState;
            }

            String userId = subscribers.get(0).getId();
            // 设置分页 获取内容
            Page page = new Page();
            if (curPage != null){
                page.setCurrentPageNo(curPage); // 设置当前页
            }

            page.setRecordTotal(5000); // 设置总条数
            List<ChangeRecord> record = changeRecordService.selectByMoney(page, userId, type);
            netState.setErrorCode(NetState.SUCCESS_CONN);
            netState.setData("查询成功");
            netState.setJson(record);
        }

        return netState;
    }

    /**
     * 购物金明细
     *
     * @param userid  用户id
     * @param curPage 页码
     */
    @RequestMapping(value = "/shopMoneyRecord", method = {RequestMethod.POST, RequestMethod.GET})
    public @ResponseBody
    NetState shopMoneyRecord(HttpServletRequest request, String userid, Integer curPage)
            throws Exception {
        // 网络状态
        NetState netState = new NetState();
        Apiinfoes apiinfoes = ApiinfoesHelper.createApiinfoes(request, "购物金明细");
        // 如果参数为空 则限制执行
        if (userid == null || userid.equals("")) {
            netState.setErrorCode(NetState.ACCOUNT_ERROR);
            netState.setData("参数丢失");
        } else {
            // 设置分页 获取内容
            Page page = new Page();
            if (curPage != null){
                page.setCurrentPageNo(curPage); // 设置当前页
            }

            page.setRecordTotal(5000); // 设置总条数
            List<ChangeRecord> record = changeRecordService.selectByShopMoney(page, userid); // 查询数据
            netState.setErrorCode(NetState.SUCCESS_CONN);
            netState.setData("查询成功");
            netState.setJson(record);
        }
//        apiinfoesService.insert(ApiinfoesHelper.setApiinfoes(apiinfoes, Produce.parmsToString(userid),
//                GsonUtil.createJsonString(netState)));
        return netState;
    }

    /**
     * 购物金明细
     *
     * @param curPage 页码
     */
    @RequestMapping(value = "/shopMoneyRecordForToken", method = {RequestMethod.POST, RequestMethod.GET})
    public @ResponseBody
    NetState shopMoneyRecordForToken(HttpServletRequest request, Integer curPage)
            throws Exception {
        // 网络状态
        NetState netState = new NetState();

        String AuthorizationToken = request.getHeader("SDB-Authorization");
        AcessToken acessToken = null;
        if (AuthorizationToken != null && !AuthorizationToken.equals("")){
            acessToken = TokenUtils.getAcessToken(AuthorizationToken);
        }
        // 如果参数为空 则限制执行
        if (acessToken == null) {
            netState.setErrorCode(NetState.SIGN_ERROR);
            netState.setData("数据签名错误");
        } else {
            String mobile = acessToken.getMobile();
            String parentId = acessToken.getParentid();
            // 验证参数
            if (mobile == null || mobile.equals("") || parentId == null || parentId.equals("")) {
                netState.setErrorCode(NetState.PARAMS_ERROR);
                netState.setData("客户端参数错误");
                return netState;
            }
            // 查询用户
            List<Subscribers> subscribers = subscribersService.selectBySub(mobile, parentId);
            if (subscribers.isEmpty()){
                netState.setErrorCode(NetState.PARAMS_ERROR);
                netState.setData("用户数据错误");
                return netState;
            }

            String userId = subscribers.get(0).getId();
            // 设置分页 获取内容
            Page page = new Page();
            if (curPage != null){
                page.setCurrentPageNo(curPage); // 设置当前页
            }

            page.setRecordTotal(5000); // 设置总条数
            List<ChangeRecord> record = changeRecordService.selectByShopMoney(page, userId); // 查询数据
            netState.setErrorCode(NetState.SUCCESS_CONN);
            netState.setData("查询成功");
            netState.setJson(record);
        }

        return netState;
    }

    /**
     * 返利明细
     *
     * @param userid  用户id
     * @param curPage 页码
     */
    @RequestMapping(value = "/rebateRecord", method = {RequestMethod.POST, RequestMethod.GET})
    public @ResponseBody
    NetState rebateRecord(HttpServletRequest request, String userid, Integer curPage)
            throws Exception {
        // 网络状态
        NetState netState = new NetState();
        Apiinfoes apiinfoes = ApiinfoesHelper.createApiinfoes(request, "返利明细");
        // 如果参数为空 则限制执行
        if (userid == null) {
            netState.setErrorCode(NetState.ACCOUNT_ERROR);
            netState.setData("参数丢失");
        } else {
            // 设置分页 获取内容
            Page page = new Page();
            if (curPage != null){
                page.setCurrentPageNo(curPage); // 设置当前页
            }
            page.setRecordTotal(rewardsService.countAll("2", null, userid, null)); // 设置总条数
            List<Rewards> record = null; // 数据
            // 当前页 > 总页数 返回null
            if (curPage > page.getPageTotal()){
                record = null;
            }else {
                record = rewardsService.selectAll(page, "2", null, userid, null); // 查询数据
            }
            netState.setErrorCode(NetState.SUCCESS_CONN);
            netState.setData("查询成功");
            netState.setJson(record);
        }
//        apiinfoesService.insert(ApiinfoesHelper.setApiinfoes(apiinfoes, Produce.parmsToString(userid),
//                GsonUtil.createJsonString(netState)));
        return netState;
    }
}
